package com.facebook;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.*;

import com.facebook.entity.Liker;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.NamedFacebookType;
import com.restfb.types.Page;
import com.restfb.types.Post;
import com.restfb.types.Post.Likes;
import com.restfb.types.User;

@SuppressWarnings("serial")
public class FirsFacebookServlet extends HttpServlet {
	private static final String MY_ACCESS_TOKEN = "AAACEdEose0cBAPaFUJjuC2nW5wadVlFQlv6KLFJuL1y8MZBXlkUXaW5HNxbNZCyfkpF4va6PyR4S13idizQUs4LbFxAiuRZAdtqyWvzzwZDZD";

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
		 com.google.appengine.api.users.User googleUser = userService.getCurrentUser();

		 boolean userAdmin = userService.isUserAdmin();
		 
		 
		if (googleUser != null) {
			resp.setContentType("text/plain");
			resp.getWriter().println("Hello google user, " + googleUser.getNickname());
			resp.getWriter().println("The user is an admin, " + userAdmin);
		
		} else {
			resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
		}

		FacebookClient facebookClient = new DefaultFacebookClient(
				MY_ACCESS_TOKEN);
		User user = facebookClient.fetchObject("me", User.class);
		Page page = facebookClient.fetchObject("codecentric.doboj", Page.class);

		resp.setContentType("text/html");
		PrintWriter writer = resp.getWriter();

		resp.getWriter().println("Hello, world");

		writer.println("User name: " + user.getName());
		writer.println("About: " + user.getAbout());
		writer.println("Bio: " + user.getBio());
		writer.println("Birthday: " + user.getBirthday());
		writer.println("Email: " + user.getEmail());
		writer.println("Gender: " + user.getGender());
		writer.println("Hometown: " + user.getHometownName());

		// ObjectifyService.register(Liker.class);
		// Objectify ofy = ObjectifyService.begin();
		// Liker liker=new Liker();
		// liker.setFirstName(user.getFirstName());
		// liker.setLastName(user.getLastName());
		// ofy.put(liker);
		//
		// writer.println("Liker was successfull saved! The id of the liker is: "
		// + liker.getId());

		Connection<Post> myLikes = facebookClient.fetchConnection("me/likes",
				Post.class);
		List<Post> data = myLikes.getData();
		for (Post post : data) {
			writer.println("<div><b>Like: </b> " + post.getName()+"</div>");
			writer.println("<div><b>Created: </b> " + post.getCreatedTime()+"</div>");
			Page page1 = facebookClient.fetchObject(post.getId(), Page.class);
			writer.println("<div><img src=\""+page1.getPicture() +"\" width=100 height=100></div>");
			writer.println("");
		}
	}
}
